/**
 * 审核弹窗
 * @author xiaoxujs
 */

import { Button, Input, message, Modal } from "antd";
import { FC, useState } from "react";
import { postPlanApprove } from "../../../api";

const { TextArea } = Input;

const ToVoidModule: FC<{
    open: boolean;
    handleCallback: () => void;
    clickId?: string
}> = ({
    open,
    handleCallback,
    clickId
}) => {
        const [approvalReason, setApprovalReason] = useState<string>(''); // 审批备注

        const handleCancel = () => {
            console.log('取消')
            setApprovalReason('');
            handleCallback()
        };
        // 驳回 同意
        const handleRejectAgree = async (approvalResult: '0' | '1') => {
            if (clickId) {
                const params = {
                    id: clickId,
                    approvalReason,
                    approvalResult,
                }
                const { code, msg } = await postPlanApprove(params) as { code: number, msg: string };
                if (code === 20000) {
                    message.success('审批成功');
                    setApprovalReason('');
                    handleCallback();
                } else {
                    message.error(msg);
                }

            }
        }
        return <Modal
            title="审批计划"
            open={open}
            onCancel={handleCancel}
            footer={[
                <Button key="reject" onClick={() => handleRejectAgree('1')}>
                    驳回
                </Button>,
                <Button key="agree" type="primary" onClick={() => handleRejectAgree('0')}>
                    同意
                </Button>,
            ]}
        >
            <TextArea rows={4} value={approvalReason} onChange={(e) => setApprovalReason(e.target.value)} />
        </Modal>
    }

export default ToVoidModule;